﻿using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;

using MB.Orm.DB;
using MB.RuleBase.Common;

using Model;
using Public;
using System.Configuration;

namespace Dao
{
    [ConfigFileName("Order")]
    public class OrderDao : Dao
    {
        private static OrderDao _NewInstance = new OrderDao();

        public static OrderDao NewInstance
        {
            get
            {
                if (_NewInstance == null)
                {
                    _NewInstance = new OrderDao();
                }

                return _NewInstance;
            }
        }
        //获取下拉选项
        public List<LookupItem> GetLookupItemsByLookupID(string lookupID)
        {
            List<LookupItem> _lstLookupItem = new List<LookupItem>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _lstLookupItem = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LookupItem>("Order", "GetLookupItemsByLookupID", lookupID);
                    return _lstLookupItem;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _lstLookupItem = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LookupItem>("Order", "GetLookupItemsByLookupID", lookupID);
                    }
                    return _lstLookupItem;
                }
            }
        }

        public List<Goods> GetAllGoodsInfo()
        {
            List<Goods> _lstGoods = null;
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _lstGoods = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Goods>("Order", "GetAllGoodsInfo");
                    return _lstGoods;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _lstGoods = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Goods>("Order", "GetAllGoodsInfo");
                    }
                    return _lstGoods;
                }
            }
        }

        public List<MachSchemeInfo> GetMachSchemeWOW()
        {
            List<MachSchemeInfo> _lstMachSchemeInfo = new List<MachSchemeInfo>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _lstMachSchemeInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<MachSchemeInfo>("Order", "GetMachSchemeWOW");
                    return _lstMachSchemeInfo;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _lstMachSchemeInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<MachSchemeInfo>("Order", "GetMachSchemeWOW");
                        return _lstMachSchemeInfo;
                    }
                    return _lstMachSchemeInfo;
                }
            }
        }

        public List<LmSendPortArea> GetLmSendPortArea()
        {
            List<LmSendPortArea> _lstMachSchemeInfo = new List<LmSendPortArea>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _lstMachSchemeInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LmSendPortArea>("Order", "GetLmSendPortArea");
                    return _lstMachSchemeInfo;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _lstMachSchemeInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LmSendPortArea>("Order", "GetLmSendPortArea");
                    }
                    return _lstMachSchemeInfo;
                }
            }
        }

        public List<OrderInfo> GetOrderInfo(string OrderNO)
        {
            List<OrderInfo> _lstOrderInfo = new List<OrderInfo>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _lstOrderInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfo>("Order", "GetOrderInfoListByOrderNO", OrderNO);
                    return _lstOrderInfo;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _lstOrderInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfo>("Order", "GetOrderInfoListByOrderNO", OrderNO);
                        return _lstOrderInfo;
                    }
                    return _lstOrderInfo;
                }
            }
        }

        public List<OrderInfo> GetOrderInfo(DateTime rationdate)
        {
            List<OrderInfo> _lstOrderInfo = new List<OrderInfo>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _lstOrderInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfo>("Order", "GetOrderInfoListByDate", rationdate);
                    return _lstOrderInfo;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _lstOrderInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfo>("Order", "GetOrderInfoListByDate", rationdate);
                        return _lstOrderInfo;
                    }
                    return _lstOrderInfo;
                }
            }
        }

        public string GetOrderNObyFboxNO(string FboxNO)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.ExecuteScalar<string>("Order", "GetOrderNOByFboxNO", FboxNO);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return "";
            }
        }

        public List<OrderInfoDetail> GetOrderDetaillstByOrderNO(string OrderNO)
        {
            List<OrderInfoDetail> _lstOrderDetailInfo = new List<OrderInfoDetail>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _lstOrderDetailInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfoDetail>("Order", "GetOrderDetaillstByOrderNO", OrderNO);
                    return _lstOrderDetailInfo;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _lstOrderDetailInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfoDetail>("Order", "GetOrderDetaillstByOrderNO", OrderNO);
                        return _lstOrderDetailInfo;
                    }
                    return _lstOrderDetailInfo;
                }
            }
        }

        public List<string> GetFboxsByOrderNO(string OrderNO)
        {
            List<string> _lst = new List<string>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _lst = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<string>("Order", "GetFboxsByOrderNO", OrderNO);
                    return _lst;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _lst = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<string>("Order", "GetFboxsByOrderNO", OrderNO);
                        return _lst;
                    }
                    return _lst;
                }
            }
        }

        public bool UpdatePindentStatus(int Status, string OrderNO)
        {
            bool _result = false;
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                     _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("FboxOrder", "UpdatePindentStatus", Status, OrderNO) > 0; 
                    return _result;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("FboxOrder", "UpdatePindentStatus", Status, OrderNO) > 0;
                        return _result;
                    }
                    return _result;
                }
            }
        }

        public bool CheckhistoryLog(string ORDERNO, string FBOXNO, int USERID, string USERNAME, string CHECKLOCATION,DateTime checkdate)
        {
            bool _result = false;
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("Order", "InsertCheckHistory", ORDERNO, FBOXNO, USERID.ToString(), USERNAME,CHECKLOCATION, checkdate) > 0;
                    return _result;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("Order", "InsertCheckHistory", ORDERNO, FBOXNO, USERID.ToString(), USERNAME, CHECKLOCATION, checkdate) > 0;
                        return _result;
                    }
                    return _result;
                }
            }
        }

        public List<FinishedOrderInfo> GetBatchFinishedOrderList(DateTime rationdate, string BatchNO, int BeginSubNO, int EndSubNO)
        {
            List<FinishedOrderInfo> _result = new List<FinishedOrderInfo>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _result = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<FinishedOrderInfo>("OrderCheck", "CheckPickedOrderInfo", rationdate, BatchNO, BeginSubNO, EndSubNO);
                    return _result;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _result = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<FinishedOrderInfo>("OrderCheck", "CheckPickedOrderInfo", rationdate, BatchNO, BeginSubNO, EndSubNO);
                        return _result;
                    }
                    return _result;
                }
            }
        }

        /// <summary>
        /// 根据订单明细的商品编号和明细拍灯状态获取订单列表
        /// </summary>
        /// <param name="GoodsCode"></param>
        /// <param name="Flag"></param>
        /// <returns></returns>
        public List<UnDealedOrderInfo> GetUndealedOrderInfo(DateTime rationdate,string batchno,string GoodsCode, string Flag = "01")
        {
            List<UnDealedOrderInfo> _result = new List<UnDealedOrderInfo>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _result = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<UnDealedOrderInfo>("OrderCheck", "GetUndealedOrderInfo", GoodsCode, Flag, rationdate, batchno);
                    return _result;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _result = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<UnDealedOrderInfo>("OrderCheck", "GetUndealedOrderInfo", GoodsCode, Flag, rationdate, batchno);
                        return _result;
                    }
                    return _result;
                }
            }
        }

        public bool PostUndealedOrderInfo(List<UnDealedOrderInfo> list)
        {
            bool _result = false;
            try
            {
                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute())
                {
                    bulk.WriteToServer("OrderCheck", "UpdateUndealedOrderInfo", list);
                }
                _result = true;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
            }
            return _result;
        }


        public List<P_INDENT_CHECK_HIS> GetOrderCheckHisInfo(string OrderNO,string FboxNO)
        {
            List<P_INDENT_CHECK_HIS> _lstOrderInfo = new List<P_INDENT_CHECK_HIS>();
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _lstOrderInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<P_INDENT_CHECK_HIS>("Order", "GetOrderInfoListByDate", OrderNO, FboxNO);
                    return _lstOrderInfo;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _lstOrderInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<P_INDENT_CHECK_HIS>("Order", "GetCheckHistoryInfo", OrderNO, FboxNO);
                        return _lstOrderInfo;
                    }
                    return _lstOrderInfo;
                }
            }
        }

        /// <summary>
        /// 根据商品编号和侧扫口及侧扫口左侧或右侧，查找该侧扫口订单需要该商品的总数量
        /// </summary>
        /// <param name="goodsSn"></param>
        /// <param name="sideSweep"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public DataSet GetTotalGoodsBySn(string goodsSn, int sideSweep, int right)
        {
            DataSet _result = new DataSet();
            string side = "%";
            if(sideSweep!=0)
                side= sideSweep.ToString();

            if (right == 1)
                side = side + ",右侧%";
            else if (right == 2)
                side = side + ",左侧%";
            else
                side = side + "%%";
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("WoChu.ERP")))
            {
                try
                {
                    _result = DatabaseExcuteByXmlHelper.NewInstance.GetDataSetByXml("OrderCheck", "GetTotalGoodsBySn", goodsSn, side);
                    return _result;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _result = DatabaseExcuteByXmlHelper.NewInstance.GetDataSetByXml("OrderCheck", "GetTotalGoodsBySn", goodsSn, side);
                        return _result;
                    }
                    return _result;
                }
            }
        }
    }
}
